/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.delaphone.flocash.web.action;

import com.delaphone.flocash.model.FUser;
import com.delaphone.flocash.service.UserService;
import com.delaphone.flocash.web.util.Validate;
import com.opensymphony.xwork2.Action;

/**
 *
 * @author tamdx
 */
public class EditAccountAction extends BaseAction {

    private UserService userService;
    private String email;
    private String address;
    private String city;
    private String state;
    private String postal_code;
    private String country;
    private String phone_number;
    private String msg;

    public String index() {
        if (user == null) {
            return Action.LOGIN;
        }
        FUser u = userService.getUserByEmail(user.getEmail());
        setEmail(u.getEmail());
        setAddress(u.getAddress());
        setCity(u.getCity());
        setState(u.getState());
        setPostal_code(u.getPostalCode());
        setCountry(u.getCountryCode());
        setPhone_number(u.getPhoneNumber());
        return Action.INPUT;
    }

    public String saveEdit() {
        if (user == null) {
            return Action.LOGIN;
        }
        FUser u = userService.getUserByEmail(user.getEmail());
        validateField();
        if (getFieldErrors().size() > 0) {
            addActionError("Some input invalid, please check again!");
        }        
        if (!isChanged(u)) {
            addActionError(getText("user.edit_contact.cantUpdate"));
        }
        if(getActionErrors().size()>0){
            request.setAttribute("address", u.getAddress());
            request.setAttribute("city", u.getCity());
            request.setAttribute("state", u.getState());
            request.setAttribute("postal_code", u.getPostalCode());
            request.setAttribute("country", u.getCountryCode());
            request.setAttribute("phone_number", u.getPhoneNumber());
            return Action.INPUT;
        }
        
        u.setAddress(getAddress());
        u.setCity(getCity());
        u.setState(getState());
        u.setPostalCode(getPostal_code());
        u.setCountryCode(getCountry());
        u.setPhoneNumber(getPhone_number());
        
        //update phone number for User logged!
        user.setPhoneNumber(getPhone_number());
        session.setAttribute("user", user);
        userService.saveChange(u);
        setMsg(getText("user.edit_contact.msg"));
        return Action.SUCCESS;

    }

    public boolean isChanged(FUser u) {
        if (!u.getAddress().equals(getAddress())) {
            return true;

        }
        if (!u.getCity().equals(getCity())) {
            return true;

        }
        if (!u.getState().equals(getState())) {
            return true;

        }
        if (!u.getPostalCode().equals(getPostal_code())) {
            return true;

        }
        if (!u.getCountryCode().equals(getCountry())) {
            return true;

        }
        if (!u.getPhoneNumber().equals(getPhone_number())) {
            return true;
        }
        return false;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getPostal_code() {
        return postal_code;
    }

    public void setPostal_code(String postal_code) {
        this.postal_code = postal_code;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void validateField() {
        if (Validate.isEmptyString(address)) {
            addFieldError("address", "*");
        } else if (Validate.isEmptyString(city)) {
            addFieldError("city", "*");
        } else if (Validate.isEmptyString(state)) {
            addFieldError("state", "*");
        } else if (Validate.isEmptyString(postal_code)) {
            addFieldError("postal_code", "*");
        } else if (Validate.isEmptyString(country)) {
            addFieldError("country", "*");
        } else if (Validate.isEmptyString(phone_number) || !Validate.isPhoneNumber(phone_number)) {
            addFieldError("phone_number", "*");
        }
    }
}
